$(function(){
    $("#gsbmfwtree").tree({
        url:$.webPath()+"/organ/bmfw/get_treejson?gsId="+$("#gsjgIdforbmfw").val(),
        onClick: function (node) {
            if($("#gsbmfwtree").tree("isLeaf",node.target)){
                $("#gsbmfwgrid").datagrid({
                    url:$.webPath()+"/sys/wzbm/get_gridjson",
                    fitColumns:true,
                    pageNumber:1,
                    fit:true,
                    queryParams: {"level": node.attributes.level, "wzbmId": node.id.split(":")[0]},
                    columns: [
                        [
                            {title: "名称", field: "name", width: 100},
                            {title: "代码", field: "code", width: 100},
                            {title: "规格型号", field: "ggxh", width: 100},
                            {title: "规格简称", field: "ggjc", width: 100},
                            {title: "材质", field: "cz", width: 100},
                            {title: "单位", field: "dw", width: 100},
                            {title: "是否报价", field: "sfbj", width: 100},
                            {title: "所属品名", field: "pm_name", width: 100}
                        ]
                    ]
                });
            }
            else{
                $("[name='search-wzbm-tree']").tree("expand",node.target);
            }
        }
    });
    var editTree = {
        allbm:$("#allbm").tree({
            url:$.webPath() + '/sys/wzbm/get_treejson',
            checkbox:true,
            onExpand:function(node){
                $(node.target).next("ul").find("li").each(function(){
                    var div = $(this).find(".tree-node");
                    var id = div.attr("node-id");
                    var bgNode = editTree.allbm.tree("find","'"+id+"'");
                    if(bgNode.attributes.bgCode){
                        div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                    }
                });
            },
            onLoadSuccess:function(node,data){
                if(!node){
                    node = $(this).tree("getRoot");
                    $(node.target).next("ul").find("li").each(function(){
                        var div = $(this).find(".tree-node");
                        var id = div.attr("node-id");
                        var bgNode = editTree.allbm.tree("find","'"+id+"'");
                        if(bgNode.attributes.bgCode){
                            div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                        }
                    });
                }
            }
        }),
        editbm:$("#editbm").tree({
            checkbox:true,
            data:[{id: "0:0", text: "编码物资", attributes: {"level": "0", "children": "false"}, state: "open",children:[]}],
            onContextMenu: function(e,node){
               e.preventDefault();
               $(this).tree('select',node.target);
               $('#bm-mm').menu('show',{
                    left: e.pageX,
                    top: e.pageY
               });
            }
        }),
        appendToTree:function(node){
            var opt = editTree.editbm.tree("options");
            if(!opt.url){
                opt.url = $.webPath() + '/sys/wzbm/get_treejson';
            }
            var parent = editTree.allbm.tree("getParent",node.target);
            if(!parent)
                return;
            var editParent = editTree.editbm.tree("find","'"+parent.id+"'");
            if(!editParent){
                editTree.appendToTree(parent);
                editParent = editTree.editbm.tree("find","'"+parent.id+"'");
            }
            var editNode = editTree.editbm.tree("find","'"+node.id+"'");
            if(!editNode){
                editTree.editbm.tree("append",{
                    parent:editParent.target,
                    data:[{
                        id:node.id,
                        text:node.text,
                        attributes:node.attributes,
                        state:node.state,
                        children:node.children
                    }]
                });
            }
        },
        dropSelect:function(node){
            var parent = editTree.editbm.tree("getParent",node.target);
            if(!parent)
                return;
            node = editTree.editbm.tree("find","'"+node.id+"'");
            if(node)
                editTree.editbm.tree("remove",node.target);
        }
    };

    $("#bm-select").click(function(){
        var checked = editTree.allbm.tree("getChecked");
        for(var i=0;i<checked.length;i++){
            editTree.appendToTree(checked[i])
        }
    });
    $("#bm-cancel").click(function(){
        var checked = editTree.editbm.tree("getChecked");
        for(var i=0;i<checked.length;i++){
            editTree.dropSelect(checked[i])
        }
    });
    $("#bm-save").click(function(){
        var root = editTree.editbm.tree("getRoot");
        var data = editTree.editbm.tree("getChildren",root.target);
        bmfwwin.mask();
        $("#bmfw-save").form("submit",{
            onSubmit: function (param) {
                if(data.length == 0){
                    bmfwwin.mask("hide");
//                    $.messager.alert("info","未选中任何物资编码");
//                    return false;
                    param["bmfwList[0].gsId"] = $("#gsjgIdforbmfw").val();
                    param["bmfwList[0].wzbmId"] = 0;
                }
                for(var i=0;i<data.length;i++){
                    param["bmfwList["+i+"].gsId"] = $("#gsjgIdforbmfw").val();
                    param["bmfwList["+i+"].wzbmId"] = data[i].id.split(":")[0];
                    param["bmfwList["+i+"].type"] = data[i].id.split(":")[1];
                }
            },
            success:function(data){
                var obj = eval('(' + data + ')');
//                alert(obj.statusCode);
                bmfwwin.mask("hide");
                if(obj.statusCode == 200){
                    $.messager.show({title:'提示',msg:"保存成功",showType:'slide'});
                    bmfwwin.window("close");
                } else {
                    $.messager.alert("info",data.message);
                }
            }
        });
    });
    $("#bm-mm .remove").click(function(){
        var node = editTree.editbm.tree('getSelected');
        var parent = editTree.editbm.tree('getParent',node.target);
        if(!parent){
            var chiledren = editTree.editbm.tree('getChildren',node.target);
            for(var i=0;i<chiledren.length;i++){
                editTree.editbm.tree('remove', chiledren[i].target);
            }
            return;
        }
        editTree.editbm.tree('remove', node.target);
    });
    $("#bm-mm .expand").click(function(){
        var node = editTree.editbm.tree('getSelected');
        editTree.editbm.tree('expand', node.target);
    });
    $("#bm-mm .collapse").click(function(){
        var node = editTree.editbm.tree('getSelected');
        editTree.editbm.tree('collapse', node.target);
    });
    $("#edit-gsbmfwtree").click(function(){
        $("#gsbmfw-tab").panel("refresh", $.webPath()+"/organ/bmfw/bmfwedit");
    });
    $("#gsbmfwtoolbar :button").click(function () {
        $('#gsbmfwgrid').datagrid({
            url:$.webPath() + "/sys/wzbm/search",
            queryParams:{
                name: $('#mc').val(),
                code: $('#code').val(),
                gsId: $("#gsjgIdforbmfw").val()
            },
            columns:[
                [
                    {title:"名称", field:"name", width:100},
                    {title:"代码", field:"code", width:100},
                    {title:"规格型号", field:"ggxh", width:100},
                    {title:"规格简称", field:"ggjc", width:100},
                    {title:"材质", field:"cz", width:100},
                    {title:"单位", field:"dw", width:100},
                    {title:"是否报价", field:"sfbj", width:100, formatter:function (value, row, index) {
                        return value ? '是' : '否'
                    }},
                    {title:"所属品名", field:"pm_name", width:100}
                ]
            ]
        });
    });
});